var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    px: ['综合','帮助人数','好评优先'], //排序列表内容
    xw: ['不限', '硕士', '博士'], //学位列表内容
    qyopen: false, //点击筛选滑动弹窗显示效果，默认不显示
    qyshow: true, //用户点击闭关区域的弹窗设置，默认不显示
    pxopen: false, //排序筛选弹窗
    nzopen: false,//学位筛选弹窗
    nzshow: true,
    pxshow: true,
    isfull: false,
    cityleft: [
      { 'provinceid': '11', 'province': '北京' }, { 'provinceid': '12', 'province': '天津' },
      { 'provinceid': '13', 'province': '河北' }, { 'provinceid': '36', 'province': '江西' },
      { 'provinceid': '15', 'province': '内蒙古' }, { 'provinceid': '21', 'province': '辽宁' },
      { 'provinceid': '22', 'province': '吉林' }, { 'provinceid': '23', 'province': '黑龙江' },
      { 'provinceid': '31', 'province': '上海' }, { 'provinceid': '32', 'province': '江苏' },
      { 'provinceid': '33', 'province': '浙江' }, { 'provinceid': '34', 'province': '安徽' },
      { 'provinceid': '35', 'province': '福建' }, { 'provinceid': '37', 'province': '山东' },
      { 'provinceid': '41', 'province': '河南' }, { 'provinceid': '42', 'province': '湖北' },
      { 'provinceid': '43', 'province': '湖南' }, { 'provinceid': '44', 'province': '广东' },
      { 'provinceid': '45', 'province': '广西' }, { 'provinceid': '46', 'province': '海南' },
      { 'provinceid': '50', 'province': '重庆' }, { 'provinceid': '52', 'province': '贵州' },
      { 'provinceid': '62', 'province': '甘肃' }, { 'provinceid': '64', 'province': '宁夏' },
      { 'provinceid': '14', 'province': '山西' }, { 'provinceid': '51', 'province': '四川' },
      { 'provinceid': '61', 'province': '陕西' }, { 'provinceid': '63', 'province': '青海' },
      { 'provinceid': '54', 'province': '西藏' }, { 'provinceid': '65', 'province': '新疆' },
      { 'provinceid': '53', 'province': '云南' }
    ],
    citycenter: {}, //筛选-左边筛选框后的显示的中间内容部分
    cityright: {}, //筛选-中间内容部分后显示的右边内容
    select1: '', //学校筛选选中后的第二个子菜单，默认显示省份下的子菜单
    select2: '', //学校筛选选择部分的中间
    select3: '', //学校筛选选择部分的右边
    selectIndex1:'',
    selectIndex2:'',
    selectIndex3:'',
    shownavindex: '',
    pxIndex: 0, //排序内容下拉框，默认第一个
    xwIndex: 0, //学位内容下拉框，默认第一个
    xwSelect:'',
    pxSelect:'',
    toturInfo :[],//查询出来的导师信息
    sortword : 'totalScore', //默认排序
    pageNumber : 1
  },

  // 地铁区域列表下拉框是否隐藏
  listqy: function (e) {
    if (this.data.qyopen) {
      this.setData({
        qyopen: false,
        nzopen: false,
        pxopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0
      })
    } else {
      this.setData({
        qyopen: true,
        pxopen: false,
        nzopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: false,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav
      })
    }
  },

  // 学位下拉框是否隐藏
  listxw: function (e) {
    if (this.data.nzopen) {
      this.setData({
        qyopen: false,
        nzopen: false,
        pxopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0
      })
    } else {
      // var xw = parseInt(e.currentTarget.dataset.nav)
      this.setData({
        nzopen: true,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: true,
        qyshow: true,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav,
        xwSelect: e.currentTarget.dataset.nav
      })
    }
  },
  // 排序下拉框是否隐藏
  listpx: function (e) {
    if (this.data.pxopen) {
      this.setData({
        qyopen: false,
        nzopen: false,
        pxopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0,
      })
    } else {
      this.setData({
        nzopen: false,
        pxopen: true,
        qyopen: false,
        nzshow: true,
        pxshow: false,
        qyshow: true,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav,
        pxSelect: e.currentTarget.dataset.nav
      })
    }
    console.log(e.target)
  },
  // 地铁区域第一栏选择内容
  selectleft: function (e) {
    console.log('用户选中的省份ID是：' + e.target.dataset.city);
    var proId = e.target.dataset.city  //省份ID
    var that = this;
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: {}
    };
    let condition = {
      operator: 1,
      code: 'provinceId',
      value: parseInt(proId),
      type: 'Number'
    };
    searchCondition.conditionList.push(condition)
    util.request(api.SelectSchool, searchCondition, 'POST', true).then(function (res) {
      console.log(res.result);
      if (res.code === 200) {
        that.setData({
          cityright: {},
          citycenter: res.result,
          select1: e.target.dataset.city,
          select2: '',
          selectIndex1: e.target.dataset.index,
          selectIndex2:''
        })
      }
    });
  },
  

  // 地铁区域中间栏选择的内容
  selectcenter: function (e) {
    console.log('用户选中的学校ID是' + e.currentTarget.dataset.city);
    var schoolId = e.currentTarget.dataset.city  //学校ID
    var that = this;
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: {}
    };
    let condition = {
      operator: 1,
      code: 'schoolId',
      value: parseInt(schoolId),
      type: 'Number'
    };
    
    searchCondition.conditionList.push(condition)
    util.request(api.SelectSpecial, searchCondition, 'POST', true).then(function (res) {
      console.log(res.result.list);
      if (res.code === 200) {
        that.setData({
          cityright: res.result.list,
          select2: e.target.dataset.city,
          select3: res.result.list[0].id,
          selectIndex2: e.target.dataset.index
        })
      }
    });
  },

  // 地铁区域左边栏选择的内容
  selectright: function (e) {
    console.log('用户选中的专业ID是' + e.currentTarget.dataset.city);
    this.setData({
      select3: e.currentTarget.dataset.city,
      selectIndex3: e.currentTarget.dataset.index
    });
  },
  // 点击灰色背景隐藏所有的筛选内容
  hidebg: function (e) {
    this.setData({
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
    })
  },
  // 地铁区域清空筛选项
  quyuEmpty: function () {
    //默认加载‘北京’下的所有大学
    this.getInitCenter();
    this.setData({
      // citycenter:'',
      cityright:'',
      select1: '',
      select2: '',
      select3: '',
      selectIndex1:'',
      selectIndex2: '-1',
      selectIndex3: '-1'
    })
  },
  // 地铁区域选择筛选项后，点击提交
  submitFilter: function () {
    console.log('选择的省份ID是：' + this.data.select1);
    console.log('选择的学校ID是：' + this.data.select2);
    console.log('选择的专业ID是：' + this.data.select3);
    var that = this;
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: {
        field: 'totalScore'
      }
    };
    let condition1 = {
      operator: 1,
      code: 'provinceId',
      value: parseInt(this.data.select1),
      type: 'Number'
    };
    let condition2 = {
      operator: 1,
      code: 'schoolId',
      value: parseInt(this.data.select2),
      type: 'Number'
    };
    let condition3 = {
      operator: 1,
      code: 'specialId',
      value: parseInt(this.data.select3),
      type: 'Number'
    };
    let condition4 = {
      operator: 1,
      code: 'itemId',
      value: 1,     //1-国内硕博 2-海外留学 3-语言考试 4-报考大学 5-校招实习
      type: 'Number'
    };
    searchCondition.conditionList.push(condition4)
    searchCondition.conditionList.push(condition1)
    searchCondition.conditionList.push(condition2)
    searchCondition.conditionList.push(condition3)
    console.log(searchCondition)
    this.getUrl(searchCondition);
    // 隐藏地铁区域下拉框
    this.setData({
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
      xwIndex:0,
      pxIndex:0,
      sortword: 'totalScore'
    })
    if (this.data.select1 == '') {
      that.setData({
        select1: 11
      })
    }
  },

  // 学位内容下拉框筛选
  selectXW: function (e) {
    console.log('学位内容下拉框筛选的内容是' + e.currentTarget.dataset.index);
    var xwId = parseInt(e.currentTarget.dataset.index) + 1;
    var that = this;
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: { field: this.data.sortword}
    };
    let condition1 = {
      operator: 1,
      code: 'provinceId',
      value: parseInt(this.data.select1),
      type: 'Number'
    };
    let condition2 = {
      operator: 1,
      code: 'schoolId',
      value: parseInt(this.data.select2),
      type: 'Number'
    };
    let condition3 = {
      operator: 1,
      code: 'specialId',
      value: parseInt(this.data.select3),
      type: 'Number'
    };
    let condition4 = {
      operator: 1,
      code: 'eduLevelId',
      value: parseInt(xwId),
      type: 'Number'
    };
    let condition5 = {
      operator: 1,
      code: 'itemId',
      value: 1,     //1-国内硕博 2-海外留学 3-语言考试 4-报考大学 5-校招实习
      type: 'Number'
    };
    searchCondition.conditionList.push(condition5)
    searchCondition.conditionList.push(condition1)
    searchCondition.conditionList.push(condition2)
    searchCondition.conditionList.push(condition3)
    if (xwId > 1 ){
      searchCondition.conditionList.push(condition4)
    }
    this.getUrl(searchCondition);
    this.setData({
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
      xwIndex: e.currentTarget.dataset.index,
      xwSelect: xwId,
    })
  },

  // 排序内容下拉框筛选
  selectPX: function (e) {
    console.log('排序内容下拉框筛选的内容是' + e.currentTarget.dataset.index);
    var sortWord = '';
    if (e.currentTarget.dataset.index == 1){
      sortWord = 'helpsNum'
    } else if (e.currentTarget.dataset.index == 2){
      sortWord = 'totalScore'
    }else{
      sortWord = ''
    };
    var that = this;
    that.setData({
      sortword: sortWord
    })

    var xwselect = this.data.xwSelect;
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: {
        field: sortWord
      }
    };
    let condition1 = {
      operator: 1,
      code: 'provinceId',
      value: parseInt(this.data.select1),
      type: 'Number'
    };
    let condition2 = {
      operator: 1,
      code: 'schoolId',
      value: parseInt(this.data.select2),
      type: 'Number'
    };
    let condition3 = {
      operator: 1,
      code: 'specialId',
      value: parseInt(this.data.select3),
      type: 'Number'
    };
    let condition4 = {
      operator: 1,
      code: 'eduLevelId',
      value: parseInt(xwselect),
      type: 'Number'
    };
    let condition5 = {
      operator: 1,
      code: 'itemId',
      value: 1,     //1-国内硕博 2-海外留学 3-语言考试 4-报考大学 5-校招实习
      type: 'Number'
    };
    searchCondition.conditionList.push(condition5)
    searchCondition.conditionList.push(condition1)
    searchCondition.conditionList.push(condition2)
    searchCondition.conditionList.push(condition3)
    if (xwselect > 1){
      searchCondition.conditionList.push(condition4)
    }
    this.getUrl(searchCondition);
    this.setData({
      pxIndex: e.currentTarget.dataset.index,
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
    })
  },

  //根据接口获取数据
  getUrl:function(e){
    var that = this;
    var searchCondition = e;
    console.log(searchCondition);
    util.request(api.SelectToturInfo, searchCondition, 'POST', true).then(function (res) {
      console.log(res.result.list);
      if (res.code === 200) {
        //设置介绍文字
        res.result.list.forEach((value, index, arr) => {
          let userEducationExperiencesTXT = "";
          for (let i in value.userEducationExperiences) {
            if (i == value.userEducationExperiences.length - 1) {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college
            } else {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college + " | "
            }
          }
          value.userEducationExperiencesTXT = userEducationExperiencesTXT.substring(0, userEducationExperiencesTXT.length)
          userEducationExperiencesTXT = "";
        })
        that.setData({
          toturInfo: res.result.list
        })
      }
    })
  },

  //默认加载‘北京’下的所有大学
  getInitCenter:function(){
    var that = this;
    let searchCondition =
      {
        pageSize: 10,
        pageNumber: 1,
        conditionList: [],
        sort: {}
      };
    let condition1 = {
      operator: 1,
      code: 'provinceId',
      value: 11,
      type: 'Number'
    };
    searchCondition.conditionList.push(condition1)
      util.request(api.SelectSchool, searchCondition, 'POST', true).then(function (res) {
      if (res.code === 200) {
        that.setData({
          citycenter: res.result,
        })
      }
    });
  },

  /* 跳转导师页面 */
  goToTeacher: function (e) {
    if (e.currentTarget.dataset.id != undefined) {
      wx.navigateTo({
        url: '../../myPages/teacher/teacher?teacherId=' + e.currentTarget.dataset.id
      })
    }
  },

  goToTeacherDetail: function (e) {
    wx.navigateTo({
      url: '../../myPages/teacherDetail/teacherDetail?teacherId=' + e.currentTarget.dataset.id
    })
  },

  goTeacherDetail:function(e){
    wx.navigateTo({
      url: '../../myPages/teacherDetail/teacherDetail'
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '国内硕博' //页面标题
    });
    this.getInitCenter();
    //初始加载导师信息
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: 1,
      conditionList: [],
      sort: {}
    };
    let condition = {
      operator: 1,
      code: 'itemId',
      value: 1,     //1-国内硕博 2-海外留学 3-语言考试 4-报考大学 5-校招实习
      type: 'Number'
    };
    searchCondition.conditionList.push(condition)
    this.getUrl(searchCondition);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var pageNum = this.data.pageNumber + 1;
    that.setData({
      pageNumber: pageNum
    })
    let searchCondition =
    {
      pageSize: 10,
      pageNumber: pageNum,
      conditionList: [],
      sort: {
        field: this.data.sortword
      }
    };
    let condition1 = {
      operator: 1,
      code: 'provinceId',
      value: parseInt(this.data.select1),
      type: 'Number'
    };
    let condition2 = {
      operator: 1,
      code: 'schoolId',
      value: parseInt(this.data.select2),
      type: 'Number'
    };
    let condition3 = {
      operator: 1,
      code: 'specialId',
      value: parseInt(this.data.select3),
      type: 'Number'
    };
    let condition4 = {
      operator: 1,
      code: 'itemId',
      value: 1,     //1-国内硕博 2-海外留学 3-语言考试 4-报考大学 5-校招实习
      type: 'Number'
    };
    let condition5 = {
      operator: 1,
      code: 'eduLevelId',
      value: parseInt(this.data.xwSelect),
      type: 'Number'
    };
    searchCondition.conditionList.push(condition1)
    searchCondition.conditionList.push(condition2)
    searchCondition.conditionList.push(condition3)
    searchCondition.conditionList.push(condition4)
    if (this.data.xwSelect > 1) {
      searchCondition.conditionList.push(condition5)
    }
    console.log(searchCondition)
    util.request(api.SelectToturInfo, searchCondition, 'POST', true).then(function (res) {
      console.log(res.result.list);
      if (res.code === 200) {
        //设置介绍文字
        res.result.list.forEach((value, index, arr) => {
          let userEducationExperiencesTXT = "";
          for (let i in value.userEducationExperiences) {
            if (i == value.userEducationExperiences.length - 1) {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college
            } else {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college + " | "
            }
          }
          value.userEducationExperiencesTXT = userEducationExperiencesTXT.substring(0, userEducationExperiencesTXT.length)
          userEducationExperiencesTXT = "";
        })
        let newArr = that.data.toturInfo.concat(res.result.list) 
        that.setData({
          toturInfo: newArr
        })
      }
    })
  },


})